#!/usr/bin/env python
# coding: utf-8

# In[ ]:


class Student:
    def __init__(self, name="", id="", course="", score=0):
        self.name = name
        self.id = id
        self.course = course
        self.score = score

    def get_name(self):
        return self.name

    def set_name(self, name):
        self.name = name

    def get_id(self):
        return self.id

    def set_id(self, id):
        self.id = id

    def get_course(self):
        return self.course

    def set_course(self, course):
        self.course = course

    def get_score(self):
        return self.score

    def set_score(self, score):
        if score < 0 or score > 100:
            print("成绩不合法！请重新输入！")
        else:
            self.score = score

class StudentRecord:
    @staticmethod
    def print_menu():
        print("=================================")
        print("欢迎使用学生成绩管理系统！")
        print("=================================")
        print("1. 记录学生成绩")
        print("2. 查询学生成绩")
        print("3. 统计课程成绩")
        print("4. 退出系统")

    @staticmethod
    def is_unique(students, id):
        for student in students:
            if student.get_id() == id:
                return False
        return True

    @staticmethod
    def is_continue():
        while True:
            choice = input("y继续，n返回菜单：").strip().lower()
            if choice == 'y':
                return True
            elif choice == 'n':
                return False
            print("输入无效，请重新输入！")

    @staticmethod
    def record_score(students):
        while True:
            name = input("请输入学生姓名：").strip()
            while True:
                id = input("请输入学生学号：").strip()
                if StudentRecord.is_unique(students, id):
                    break
                print("学号重复！请重新输入！")
            course = input("请输入学生课程：").strip()
            while True:
                try:
                    score = float(input("请输入学生成绩：").strip())
                    if 0 <= score <= 100:
                        break
                    print("成绩不合法！请输入0-100之间的数字。")
                except ValueError:
                    print("输入无效！请输入数字。")
            student = Student(name, id, course, score)
            students.append(student)
            print("成绩已经成功记录！")
            if not StudentRecord.is_continue():
                break

    @staticmethod
    def query_score(students):
        while True:
            name = input("请输入学生姓名：").strip()
            id = input("请输入学生学号：").strip()
            found = False
            for i, student in enumerate(students, 1):
                if student.get_name() == name and student.get_id() == id:
                    print("=================================")
                    print(f"序号：{i}")
                    print(f"学生姓名：{student.get_name()}")
                    print(f"学生学号：{student.get_id()}")
                    print(f"学生课程：{student.get_course()}")
                    print(f"学生成绩：{student.get_score()}")
                    print("=================================")
                    found = True
            if not found:
                print("没有该学生的成绩！")
            if not StudentRecord.is_continue():
                break

    @staticmethod
    def statistics_score(students):
        while True:
            course = input("请输入课程名称：").strip()
            count = 0
            total = 0
            max_score = float('-inf')
            min_score = float('inf')
            for student in students:
                if student.get_course() == course:
                    count += 1
                    total += student.get_score()
                    if student.get_score() > max_score:
                        max_score = student.get_score()
                    if student.get_score() < min_score:
                        min_score = student.get_score()
            if count == 0:
                print("没有该课程的成绩！")
            else:
                average = total / count
                print("=================================")
                print(f"课程名称：{course}")
                print(f"该课程的平均分为：{average}")
                print(f"该课程的最高分为：{max_score}")
                print(f"该课程的最低分为：{min_score}")
                print("=================================")
            if not StudentRecord.is_continue():
                break

    @staticmethod
    def exit_system():
        print("已退出系统！")
        exit()

    @staticmethod
    def get_menu_choice():
        while True:
            try:
                choice = int(input("请输入菜单选项的序号：").strip())
                if 1 <= choice <= 4:
                    return choice
                print("无效的选择！请输入1-4之间的数字。")
            except ValueError:
                print("输入无效！请输入数字。")

    @staticmethod
    def main():
        students = []
        # 初始化一些测试数据
        students.append(Student("lhy", "8120", "java", 90))
        students.append(Student("cxy", "8121", "java", 91))
        students.append(Student("pyt", "8122", "java", 92))
        students.append(Student("sky", "8123", "java", 93))

        while True:
            print()
            StudentRecord.print_menu()
            print()

            choice = StudentRecord.get_menu_choice()

            # 根据用户选择执行相应操作
            if choice == 1:
                StudentRecord.record_score(students)
            elif choice == 2:
                StudentRecord.query_score(students)
            elif choice == 3:
                StudentRecord.statistics_score(students)
            elif choice == 4:
                StudentRecord.exit_system()

if __name__ == "__main__":
    StudentRecord.main()


# In[ ]:




